#!/usr/bin/python

print '\nWeb Security Checker 1.1.5 by Hugo Ramos\n'

import sys
import httplib
import string
import getopt
import fileinput
import glob

options = 'hno:p:s:t:xf'
longoptions = ['help','normal','proxy=','port=','server=','proxy-port=','extensive','extensive-full']
lst = fileinput.input(glob.glob('*.dic'))
helpfile = open('helpfile.txt', 'r')


def usage():
	ajuda = helpfile.read()
	helpfile.close
	print ajuda


def main():
	try:
		opts, args = getopt.getopt(sys.argv[1:], options, longoptions)
	except:
		print 'An error ocurred! Please verify the option list.\n\n'
		usage()
		sys.exit()

	if len(opts) != 0:
		tipo = 1
		server = ''
		port = ''
		proxy = ''
		pp = ''
		for o, a in opts:
			if o in ("-h", "--help"):
				usage()
				sys.exit()
			if o in ("-n", "--normal"):
				tipo = 1
			if o in ("-o", "--proxy"):
				proxy = a
			if o in ("-p", "--port"):
				port = int(a)
			else:
				port = 80
			if o in ("-s", "--server"):
				server = a
			if o in ("-t", "--proxy-port"):
				pp = int(a)
			if o in ("-x", "--extensive"):
				tipo = 2
			if o in ("-f", "--extensive-full"):
				tipo = 3

		if server != '':
			try:
				if proxy:
					if pp:
						v1 = httplib.HTTP(proxy, pp)
					else:
						pp = 8080
						v1 = httplib.HTTP(proxy, pp)
					v1.putrequest('GET', 'http://' + server)
				else:
					v1 = httplib.HTTP(server, port)
					v1.putrequest('GET','/')
				v1.putheader('Accept','text/html')
				v1.putheader('Accept','text/plain')
				v1.endheaders()

				errcode = ''
				errmsg = ''
				headers = ''

				errcode, errmsg, headers = v1.getreply()

				print "============================================================================\nServer: " + server
				print "Server reply: " + str(errcode), errmsg
				print str(headers) + '============================================================================\n'

			except:
				print 'Could not check server headers!\n\n'

			for word in lst:
				'''sys.stdout.write("--> ")
				sys.stdout.write(word)'''
				
				word = string.replace(word, '\n', '')
				url = 'http://' + server + ':' + str(port) + '/' + word

				try:
					if proxy:
						if pp:
							v2 = httplib.HTTP(proxy, pp)
						else:
							pp = 8080
							v2 = httplib.HTTP(proxy, pp)
						v2.putrequest('GET',url)
					else:
						v2 = httplib.HTTP(server, port)
						v2.putrequest('GET','/' + word)
					v2.putheader('Accept','text/html')
					v2.putheader('Accept','text/plain')
					v2.endheaders()

					errcode, errmsg, headers = v2.getreply()

				except:
					print '\nError! The specified server could not be checked!\n'
					sys.exit()

				if tipo == 1:
					if errcode == 200:
						print url
						print '--> ' + str(errcode) + ' ' + errmsg

				if tipo == 2:
					if errcode != 404:
						print url
						print '--> ' + str(errcode) + ' ' + errmsg
						
				if tipo == 3:
					if errcode != 404:
						print '\n============================================================================'
						print url
						print '--> ' + str(errcode) + ' ' + errmsg
						print '============================================================================\n'
					else:
						print url
						print '--> ' + str(errcode) + ' ' + errmsg

			print '\nDone! :-)\n\n'

		else:
			print 'An error ocurred!\nPlease verify the option list.\n\n'
			usage()
			sys.exit()

	else:
		usage()
		sys.exit()


if __name__ == "__main__":
	main()

